package cate.game.role.pvp.throne;

import cate.common.table.activity.tbboss.TBBossGroupRow;
import cate.common.table.activity.tbboss.TBBossHpRwdRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.game.play.param.HpExtend;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.pvp.throne.TBZoneBossFTC;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarm;
import cate.game.role.pvp.throne.msg.MyZoneBossResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashSet;
import java.util.Set;

@NoteClass("王座争霸全服boss")
public class RoleTBZoneBoss extends AbstractFarm {

	@NoteField(value = "分组", detail = "区别于选拔赛分组赛的分组")
	public int group;

	@NoteField("怪物血量继承")
	public HpExtend hpExtend;

	@NoteField("挑战次数")
	public int challengeTime;

	@NoteField("boss被击杀了")
	public boolean kill;

	@NoteField("领取的奖励id")
	public Set<Integer> rwdGot;



	@Override
	public int getFuncId() {
		return GDFunc.THRONE_BOSS;
	}

	@Override
	public void onAfterOpen() {

	}

	@Override
	public int getFightFuncId() {
		return GDFunc.THRONE_BOSS;
	}

	public void onSeasonChange() {
		clear();
	}

	@Override
	public void initialize(Role role) {
		super.initialize(role);
		if (hpExtend == null) {
			hpExtend = new HpExtend();
		}
		if (rwdGot == null) {
			rwdGot = new HashSet<>();
		}
	}

	public void clear() {
		group = 0;
		hpExtend.clear();
		challengeTime = 0;
		kill = false;
		rwdGot.clear();
	}

	public EcResult<Void> challenge(boolean sim) {
		EcResult<Void> r = new EcResult<>();
		if (isFighting()) {
			return r.fail("当前正在战斗中");
		}
		if (!game().pvp.throne.data.holder.base.timeTable.inZoneBoss()) {
			return r.fail("当前无法挑战");
		}
		TBBossGroupRow bossRow = game().table.tbBoss.group.get(group);
		if (bossRow == null) {
			return r.fail("boss配置不存在");
		}
		if (challengeTime >= bossRow.challengeMax) {
			return r.fail("挑战次数不足");
		}
		if (kill) {
			return r.fail("boss已被击杀");
		}
		EcResult<FightSide> sideB_r = game().fight.side.createFightSideByCode(GDFight.Team.B, bossRow.insCode);
		if (!sideB_r.ok()) {
			return r.fail(sideB_r.message);
		}
		FightSide sideB = sideB_r.data;
		TBZoneBossFTC ftc = new TBZoneBossFTC(role, sim, bossRow);
		ftc.setPositionApply(GDFunc.GRADE_PVP);
		ftc.param.sideB.hpExtend = hpExtend;
		EcResult<PlayPO> play_r = game().play.startByRole(role, ftc, sideB);
		if (!play_r.ok()) {
			return r.fail(play_r.message);
		}
		this.startFighting();
		return r.success();
	}

	@JsonIgnore
	public EcResult<Void> getRwd(int tid) {
		EcResult<Void> r = new EcResult<>();
		TBBossHpRwdRow rwdRow = game().table.tbBoss.hpRwd.get(tid);
		if (rwdRow == null) {
			return r.fail("奖励不存在");
		}
		if (rwdRow.group != group) {
			return r.fail("奖励分组有误");
		}
		if (rwdGot.contains(tid)) {
			return r.fail("奖励已领过");
		}
		TBBossGroupRow bossRow = game().table.tbBoss.group.get(group);
		if (bossRow == null) {
			return r.fail("boss配置不存在");
		}
		double hpRate = hpExtend.getTidRates().getOrDefault(bossRow.bossTid, 1d);
		if (hpRate > rwdRow.hpRate) {
			return r.fail("血量有误");
		}
		rwdGot.add(tid);
		MixRes rwd = new MixRes(rwdRow.rwdStr);
		if (game().pvp.throne.data.holder.boss.kill) {
			rwd.addList(new MixRes(rwdRow.killRwdStr));
		}
		new MixResAdder().setRes(rwd).setOperation(GDOperation.THRONE_ZONE_BOSS).setDialogReward(true).exe(role);
		noticeUpdate();
		return r.success();
	}

	public void noticeUpdate() {
		role.sendNow(new MyZoneBossResp(role));
	}
}
